
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Bannerclient;

public class GenericBannerClientDAO extends AutoKeys implements
		org.jetnuke.dao.BannerClientDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.BannerClientModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerClientModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Bannerclient u = new Bannerclient();
			fillBannerClientFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}
	/**
	 * @param u
	 * @param rs
	 */
	private void fillBannerClientFromResultSet(Bannerclient u, ResultSet rs) throws Exception{
		
		u.setCID(rs.getInt("CID"));
		u.setCONTACT(rs.getString("CONTACT"));
		u.setEMAIL(rs.getString("EMAIL"));
		u.setEXTRAINFO(rs.getString("EXTRAINFO"));
		u.setLOGIN(rs.getString("LOGIN"));
		u.setNAME(rs.getString("NAME"));
		u.setPASSWD(rs.getString("PASSWD"));
		
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.BannerClientModel#countActiveForClient(int)
	 */
	public int countActiveForClient(int cid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerClientModel.countForClient"));
		p.setInt(1,cid);
		p.setInt(2,1);//Active
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.BannerClientModel#countInactiveForClient(int)
	 */
	public int countInactiveForClient(int cid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerClientModel.countForClient"));
		p.setInt(1,cid);
		p.setInt(2,0);//Inactive
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}
	public int addNew(Bannerclient bn) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("BannerClientModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("BannerClientModel.lastGeneratedBannerClientID"));
		initNewBannerClient(bn, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
	}
	/**
	 * @param bn
	 * @param p
	 */
	private void initNewBannerClient(Bannerclient bn, PreparedStatement p)throws Exception {
		p.setString(1,bn.getCONTACT());
		p.setString(2,bn.getEMAIL());
		p.setString(3,bn.getEXTRAINFO());
		p.setString(4,bn.getLOGIN());
		p.setString(5,bn.getNAME());
		p.setString(6,bn.getPASSWD());
		
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.BannerClientModel#deleteByID(int)
	 */
	public void deleteByID(int cid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerClientModel.deleteByID"));
		p.setInt(1,cid);
		p.executeUpdate();
		p.close();
		
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.BannerClientModel#retrieveByPK(int)
	 */
	public Bannerclient retrieveByPK(int cid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerClientModel.selectByID"));
		p.setInt(1, cid);
		
		ResultSet rs = p.executeQuery();
		Bannerclient s = new Bannerclient();
		
		if (rs.next()) {
			fillBannerClientFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.BannerClientModel#update(org.jetnuke.entities.Bannerclient)
	 */
	public void update(Bannerclient client) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerClientModel.update"));
		p.setString(1,client.getCONTACT());
		p.setString(2,client.getEMAIL());
		p.setString(3,client.getEXTRAINFO());
		p.setString(4,client.getLOGIN());
		p.setString(5,client.getNAME());
		p.setString(6,client.getPASSWD());
		p.setInt(7,client.getCID());
		p.executeUpdate();
		
	}
}
